/*
 *    Copyright (c) 2018-2025, boss All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the boss developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: boss
 */
package com.hopes.bizz.enforcement.hid.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hopes.bizz.enforcement.common.constant.HidDicConstant;
import com.hopes.bizz.enforcement.hid.dto.RiskHidHiddenDangerDelayDTO;
import com.hopes.bizz.enforcement.hid.entity.RiskHidHandleRecordEntity;
import com.hopes.bizz.enforcement.hid.entity.RiskHidHiddenDangerDelayEntity;
import com.hopes.bizz.enforcement.hid.entity.RiskHidHiddenDangerEntity;
import com.hopes.bizz.enforcement.hid.mapper.RiskHidHiddenDangerDelayMapper;
import com.hopes.bizz.enforcement.hid.service.RiskHidHandleRecordService;
import com.hopes.bizz.enforcement.hid.service.RiskHidHiddenDangerDelayService;
import com.hopes.bizz.enforcement.hid.service.RiskHidHiddenDangerService;
import com.hopes.boss.common.security.util.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * ${tableComment}
 *
 * @author pig
 * @date 2023-07-19 10:14:58
 */
@Service
public class RiskHidHiddenDangerDelayServiceImpl extends ServiceImpl<RiskHidHiddenDangerDelayMapper, RiskHidHiddenDangerDelayEntity> implements RiskHidHiddenDangerDelayService {

    @Autowired
    private RiskHidHandleRecordService riskHidHandleRecordService;

    @Autowired
    @Lazy
    private RiskHidHiddenDangerService riskHidHiddenDangerService;


    @Override
    public void saveDelay(RiskHidHiddenDangerDelayDTO dangerDelayDTO) {
        RiskHidHiddenDangerDelayEntity delayEntity = new RiskHidHiddenDangerDelayEntity();
        BeanUtils.copyProperties(dangerDelayDTO, delayEntity);
        // 设置处理人
        Long handleUserId = riskHidHandleRecordService.lambdaQuery()
                .eq(RiskHidHandleRecordEntity::getHidId, dangerDelayDTO.getHiddenDangerId())
                .and(item ->
                        item.eq(RiskHidHandleRecordEntity::getNodeCode, HidDicConstant.HID_DANGER_STATUS_WAIT_CONFIRM)
                        .or()
                        .eq(RiskHidHandleRecordEntity::getNodeCode, HidDicConstant.HID_DANGER_STATUS_WAIT_SUBMIT))
                .orderByDesc(RiskHidHandleRecordEntity::getHandleDate)
                .last("limit 1").one().getHandleUserId();
        delayEntity.setHandleUserId(handleUserId.toString());
        // 申请人
        delayEntity.setApplyUserId(SecurityUtils.getUserId());
        // 申请时间
        delayEntity.setApplyTime(new Date());
        // 处理状态
        delayEntity.setHandleStatus(HidDicConstant.RISK_HID_HIDDEN_DANGER_DELAY_UNDISPOSED);
        // 更改隐患的延期状态为申请中
        RiskHidHiddenDangerEntity hiddenDangerEntity = riskHidHiddenDangerService.getById(dangerDelayDTO.getHiddenDangerId());
        hiddenDangerEntity.setDelayStatus(HidDicConstant.DELAY_STATUS_APPLY);
        riskHidHiddenDangerService.updateById(hiddenDangerEntity);
        save(delayEntity);
    }

    @Override
    public void updateHidAndDelay(RiskHidHiddenDangerDelayEntity hidHiddenDangerDelay) {
        hidHiddenDangerDelay.setHandleUserId(SecurityUtils.getUserIdStr());
        hidHiddenDangerDelay.setHandleTime(new Date());
        hidHiddenDangerDelay.setHandleStatus(HidDicConstant.RISK_HID_HIDDEN_DANGER_DELAY_DISPOSE);
        // 更新延期记录
        updateById(hidHiddenDangerDelay);
        Integer handleResult = hidHiddenDangerDelay.getHandleResult();
        Long hiddenDangerId = hidHiddenDangerDelay.getHiddenDangerId();
        RiskHidHiddenDangerEntity entity = riskHidHiddenDangerService.getById(hiddenDangerId);
        // 通过 修改隐患延期状态，和整改期限
        if (handleResult.equals(HidDicConstant.RISK_HID_HIDDEN_DANGER_DELAY_PASS)) {
            entity.setDelayStatus(HidDicConstant.DELAY_STATUS_PASS);
            entity.setCorrectiveDeadline(hidHiddenDangerDelay.getDelayDate());
        } else if (handleResult.equals(HidDicConstant.RISK_HID_HIDDEN_DANGER_DELAY_REJECT)) {
            // 驳回 修改隐患延期状态，不修改整改期限
            entity.setDelayStatus(HidDicConstant.DELAY_STATUS_REJECT);
        }
        riskHidHiddenDangerService.updateById(entity);

    }
}
